package com.fingard.cluster;

import java.io.File;
import java.util.ArrayList;

import com.fingard.FGBiz;
import com.fingard.client.DspClient;
import com.fingard.diagnostics.LogHelper;
import com.fingard.dsp.bank.syncfile.FileItem;
import com.fingard.io.FileHelper;
import com.fingard.net.TcpReceiver;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;

/**DSP集群*/
public class ClusterCenter {
	/**其它dsp*/
	public ArrayList<HostInfo> otherHostList;
	/**是否开启*/
	public boolean enable;
	public boolean isEnable(){
		return enable;
	}
	public String ownName;
	public String ownPublicKey;
	public String ownPrivateKey;
	
	public ClusterCenter(){
		enable = false;
		loadFromXml();
	}
	
	private void loadFromXml(){
		otherHostList = new ArrayList<HostInfo>();
		String filePath = StringHelper.trimEndFileSp(FGBiz.DSPHOME) + File.separator + "config"+File.separator+"Cluster.xml";
		if(FileHelper.existFile(filePath)){
			try{
				String configText = FileHelper.readFileText(filePath, "utf-8");
				XmlTextReader reader = new XmlTextReader(configText);
				if("true".equals(reader.getSimpleTextNotNull("enable").toLowerCase())){
					enable = true;
				}else{
					enable = false;
				}
				ownName = reader.getSimpleTextNotNull("ownName");
				ownPublicKey = reader.getSimpleTextNotNull("ownPublicKey");
				ownPrivateKey = reader.getSimpleTextNotNull("ownPrivateKey");
				
				while(reader.read("/config/host")){
					HostInfo eachHost = new HostInfo(reader.readedXElement);
					if(eachHost.enable){
						if((eachHost.ip.equalsIgnoreCase(FGBiz.localHostIp)||"127.0.0.1".equalsIgnoreCase(eachHost.ip)) && eachHost.port == FGBiz.dspCenter.dspSet.listenerPort){
							FGBiz.startLog.addLogNoTime("DSP Cluster: "+eachHost.addr+"(filter out)");
						}else{
							otherHostList.add(eachHost);
							FGBiz.startLog.addLogNoTime("DSP Cluster: "+eachHost.addr);
						}
					}
				}
				if(otherHostList.size() == 0){
					enable = false;
				}
			}catch(Exception e){
				enable = false;
				FGBiz.limitMsg.exception("ClusterCenter", e);
			}
		}
	}
	
	/**同步文件*/
	public String syncFile(FileItem pFileItem){
		String logCode = "ClusterCenter.syncFile";
		StringBuilder retSb = new StringBuilder();
		if(enable && otherHostList!= null && otherHostList.size()>0){
			for(HostInfo eachHost : otherHostList){
				String respCode = "";
				String respInfo = "";
				String retXml = "";
				DspClient dspClient = new DspClient(eachHost.addr);
				try {
					TcpReceiver retReceiver = dspClient.syncFile(pFileItem.localFilePath, pFileItem.fileType, pFileItem.srcSys, pFileItem.bank, pFileItem.relativePath);
					if(retReceiver.isCompleted){
						retXml = retReceiver.getContentString(dspClient.charsetName);
						if(retXml.contains("<") && retXml.contains(">")){
							XmlTextReader retXmlRd = new XmlTextReader(retXml);
							respCode = retXmlRd.getSimpleText("RespCode");
							respInfo = retXmlRd.getSimpleText("RespInfo");
						}
					}else{
						respInfo = retReceiver.getConnMsg();
					}
				} catch (Exception e) {
					respInfo = LogHelper.getStackTrace(e);
				}
				
				if("0".equals(respCode)){
					retSb.append("同步至"+eachHost.addr+"成功："+respInfo+"\r\n");
				}else{
					String aLog = "同步至"+eachHost.addr+"失败："+respInfo+"，响应报文："+retXml+"\r\n";
					retSb.append(aLog);
					FGBiz.limitMsg.error(logCode, aLog);
				}
			}
		}
		return retSb.toString();
	}
}
